//
//  AppDelegate.swift
//  ERecycle-Shop
//
//  Created by abb on 16/8/10.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift
let ABBRootViewControllerKey = "ABBRootViewControllerKey"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // 创建window并初始化根控制器
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window!.backgroundColor = UIColor.whiteColor()
        window!.rootViewController = defaultController()
        window!.makeKeyAndVisible()
        // 
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UINavigationBar.appearance().barTintColor = UIColor.tintColor()
        // 注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.abbRootViewController(_:)), name: ABBRootViewControllerKey, object: nil)
        // 验证高德SDKKey
        AMapServices.sharedServices().apiKey = "d0ed14f562e076aa125ee227f20f27f7"
        // IQKeyboardManagerSwift
        IQKeyboardManager.sharedManager().enable = true
        IQKeyboardManager.sharedManager().shouldResignOnTouchOutside = true
        return true
    }
    
    /**
     响应通知更改根控制器
     */
    func abbRootViewController(notify:NSNotification) {
//        if notify.object as! Bool {
//            
//            window?.rootViewController = HomeViewController()
//            
//        } else {
//            // 备用
            window?.rootViewController = defaultController()
//        }
    }
    
    
    func defaultController() -> UIViewController {
        
        if User.userLogin() {
            let homeNav = AbbNavigationController(rootViewController: HomeViewController())
            homeNav.navigationBar.tintColor = UIColor.tintColor()
            return homeNav
        }
        let loginNav = AbbNavigationController(rootViewController: LoginViewController())
        return loginNav
    }
    
    var dictionaryConstraints = [NSString:NSArray]()
    

    
    func application(application: UIApplication, willChangeStatusBarFrame newStatusBarFrame: CGRect) {
        print("newStatusBarFrame:\(newStatusBarFrame)")
        
        if newStatusBarFrame.size.height > 20.0 {
            for window in UIApplication.sharedApplication().windows {
                if window.classForCoder.description() == "UITextEffectsWindow" || window.classForCoder.description() == "UIRemoteKeyboardWindow" {
                    var constraints = [NSLayoutConstraint]()
                    for constraint in window.constraints {
                        if constraint.description.containsString("V:|-(0)-[UIInputSetContainerView") {
                            print("\(window.classForCoder.debugDescription), \(constraint.description), \(constraint.priority), \(constraint.constant)")
                            constraints.append(constraint)
                            window.removeConstraint(constraint)
                        }
                    }
                    
                    if constraints.count > 0 {
                        
                        dictionaryConstraints[NSString(format: "%p", unsafeAddressOf(window))] = constraints
                    }
                }
            }
        }
    }
    
    func resetConstraints() {
        for window in UIApplication.sharedApplication().windows {
            if window.classForCoder.description() == "UITextEffectsWindow" || window.classForCoder.description() == "UIRemoteKeyboardWindow" {
                if dictionaryConstraints.count > 0 {
                    let keys = Array(dictionaryConstraints.keys)
                    
                    for i in 0 ..< keys.count {
                        if NSString(format: "%p", unsafeAddressOf(window)) == keys[i] {
                            window.addConstraints(dictionaryConstraints[keys[i]] as! [NSLayoutConstraint])
                        }
                    }
                }
            }
        }
    }
    func application(application: UIApplication, didChangeStatusBarFrame oldStatusBarFrame: CGRect) {
        print("oldStatusBarFrame: \(oldStatusBarFrame)")
        
        if oldStatusBarFrame.size.height > 20.0 {
            if dictionaryConstraints.count > 0 {
                self.resetConstraints()
                dictionaryConstraints.removeAll()
            }
        }
        
        for window in UIApplication.sharedApplication().windows {
            if window.classForCoder.description() == "UITextEffectsWindow" || window.classForCoder.description() == "UIRemoteKeyboardWindow" {
                for constraint in window.constraints {
                    if constraint.description.containsString("V:|-(0)-[UIInputSetContainerView") {
                        print("\(window.classForCoder.debugDescription), \(constraint.description), \(constraint.priority), \(constraint.constant)")
                    }
                    
                }
            }
        }
    }


}

